export function compileShader(gl, shaderSource, shaderType) {
  const shader = gl.createShader(shaderType);
  gl.shaderSource(shader, shaderSource);
  gl.compileShader(shader);
  if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
    throw new Error(`Shader compile failed with: ${gl.getShaderInfoLog(shader)}`);
  }
  return shader;
}

// 初始化着色器
export function initShaders(gl, v, f) {
  const s1 = compileShader(gl, v, gl.VERTEX_SHADER);
  const s2 = compileShader(gl, f, gl.FRAGMENT_SHADER);

  const program = gl.createProgram();
  gl.attachShader(program, s1);
  gl.attachShader(program, s2);
  gl.linkProgram(program);

  gl.useProgram(program);

  gl.program = program;
}
